//
//  NewsViewController.m
//  NewProject
//
//  Created by admin on 2017/6/15.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsTableViewCell.h"
@interface NewsViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UITableView *leftTableView;
@property(nonatomic,strong)UITableView *rightTableView;
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,strong)UIView *switchView;
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic, strong)UITapGestureRecognizer *gestureRecognize;
@property(nonatomic,strong)UILabel *aLabel;
@property(nonatomic,strong)UILabel *bLabel;

@property(nonatomic,strong)UIImageView *noDateView;
@end

@implementation NewsViewController
- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB0X(0xF8F9F6);
    self.title = @"我的消息";
    
    _dataArray = @[@{kTitle:@"亲爱的用户，恭喜您成功注册橙子社区，成为会员",kTime:@"2017-2-24",kType:@"0"},@{kTitle:@"亲爱的用户，恭喜您成功注册橙子社区，成为会员",kTime:@"2017-2-25",kType:@"1"},@{kTitle:@"亲爱的用户，恭喜您成功注册橙子社区，成为会员",kTime:@"2017-2-26",kType:@"1"}];
    [self createSwitchView];
    [self createTableView];
    // Do any additional setup after loading the view.
}

- (void)createSwitchView{
    _switchView = [[UIView alloc]initWithFrame:CGRectMake(0,  9/WIDTH_6_SCALE, WIDTH, 52/WIDTH_6_SCALE)];
    _switchView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_switchView];
    
    _aLabel = [[UILabel alloc]init];
    _aLabel.frame = CGRectMake(70/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 70/WIDTH_6_SCALE, 15/WIDTH_6_SCALE);
    _aLabel.font = FONT(15);
    _aLabel.textColor = RGB0X(0x333333);
    _aLabel.text = @"系统消息";
    [_switchView addSubview:_aLabel];
    
    _bLabel = [[UILabel alloc]init];
    _bLabel.frame = CGRectMake(245/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 70/WIDTH_6_SCALE, 15/WIDTH_6_SCALE);
    _bLabel.font = FONT(15);
    _bLabel.text = @"用户消息";
    _bLabel.textColor = RGB0X(0x333333);
    [_switchView addSubview:_bLabel];

    self.lineView = [[UIView alloc]init];
    self.lineView.frame = CGRectMake(70/WIDTH_6_SCALE, 48/WIDTH_6_SCALE, 60/WIDTH_6_SCALE, 2/WIDTH_6_SCALE);
    self.lineView.backgroundColor = RGB0X(0xFFD601);
    [_switchView addSubview:self.lineView];
    
    _gestureRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gestureRecognizeClick:)];
    [_switchView addGestureRecognizer:_gestureRecognize];
}

- (void)gestureRecognizeClick:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.switchView];
    NSInteger index =  point.x/(WIDTH/2);
    if (index == 0) {
        _aLabel.textColor = RGB0X(0x272727);
        _bLabel.textColor = RGB0X(0x999999);
    }else{
        _aLabel.textColor = RGB0X(0x999999);
        _bLabel.textColor = RGB0X(0x272727);
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.lineView.frame = CGRectMake(70/WIDTH_6_SCALE+175/WIDTH_6_SCALE*index, 48/WIDTH_6_SCALE, 60/WIDTH_6_SCALE, 2/WIDTH_6_SCALE);
    }];
    [_scrollView setContentOffset:CGPointMake(WIDTH*index, 0) animated:YES];
}

- (void)createTableView{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 65/WIDTH_6_SCALE, WIDTH, HIGHT-65/WIDTH_6_SCALE-64)];
    _scrollView.contentSize = CGSizeMake(2*WIDTH, HIGHT-65/WIDTH_6_SCALE-64);
    _scrollView.delegate = self;
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    [self.view addSubview:_scrollView];
    
    _leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT-65/WIDTH_6_SCALE-64)];
    _leftTableView.dataSource = self;
    _leftTableView.delegate = self;
    _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _leftTableView.backgroundColor = RGB0X(0xF1F1F1);
    _leftTableView.rowHeight = 74/WIDTH_6_SCALE;
    [_scrollView addSubview:_leftTableView];
    
    _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HIGHT-65/WIDTH_6_SCALE-64)];
    _rightTableView.dataSource = self;
    _rightTableView.delegate = self;
    _rightTableView.rowHeight = 74/WIDTH_6_SCALE;
    _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _rightTableView.backgroundColor = RGB0X(0xF1F1F1);
    [_scrollView addSubview:_rightTableView];
    
    _noDateView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT-65/WIDTH_6_SCALE-64)];
    _noDateView.image = [UIImage imageNamed:@"没数据"];
    [_scrollView addSubview:_noDateView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CellIdentifier = SF(@"%ld",indexPath.row);
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index =  scrollView.contentOffset.x/WIDTH;
    if (index == 0) {
        _aLabel.textColor = RGB0X(0x272727);
        _bLabel.textColor = RGB0X(0x999999);
    }else{
        _aLabel.textColor = RGB0X(0x999999);
        _bLabel.textColor = RGB0X(0x272727);
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.lineView.frame = CGRectMake(70/WIDTH_6_SCALE+175/WIDTH_6_SCALE*index, 48/WIDTH_6_SCALE, 60/WIDTH_6_SCALE, 2/WIDTH_6_SCALE);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
